package com.example.a11
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.provider.MediaStore
import android.util.Log

class MusicService : Service() {
    companion object {
        const val Command = "operate"
    }
    private val mediaPlayer: MediaPlayer = MediaPlayer()
    private val musicList: MutableList<String> = mutableListOf()
    private val musicNameList: MutableList<String> = mutableListOf()
    private var current = 0
    private val binder = MusicBinder()
    private lateinit var notificationManager: NotificationManager
    private val CHANNEL_ID = "music_service_channel"

    inner class MusicBinder : Binder() {
        val musicName: String
            get() = musicNameList.getOrElse(current) { "Unknown" }

        var currentPosition: Int
            get() = mediaPlayer.currentPosition
            set(value) = mediaPlayer.seekTo(value)

        val duration: Int
            get() = mediaPlayer.duration

        val size: Int
            get() = musicList.size

        val currentIndex: Int
            get() = current

        fun play() {
            if (musicList.isEmpty()) return
            val path = musicList[current]
            mediaPlayer.reset()
            mediaPlayer.setDataSource(path)
            mediaPlayer.prepareAsync()
            mediaPlayer.setOnPreparedListener { it.start() }
            mediaPlayer.setOnCompletionListener { next() }
        }

        fun pause() {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.pause()
            } else {
                mediaPlayer.start()
            }
        }

        fun stop() {
            mediaPlayer.stop()
            stopSelf()
        }

        fun next() {
            current = (current + 1) % musicList.size
            play()
        }

        fun prev() {
            current = if (current - 1 < 0) musicList.size - 1 else current - 1
            play()
        }
    }

    override fun onCreate() {
        super.onCreate()
        createNotificationChannel()
        getMusicList()
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                "Music Service Channel",
                NotificationManager.IMPORTANCE_LOW
            )
            notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    override fun onBind(intent: Intent): IBinder {
        return binder
    }

    @SuppressLint("Range")
    private fun getMusicList() {
        val cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null)
        cursor?.use {
            while (it.moveToNext()) {
                val musicPath = it.getString(it.getColumnIndex(MediaStore.Audio.Media.DATA))
                musicList.add(musicPath)
                val musicName = it.getString(it.getColumnIndex(MediaStore.Audio.Media.TITLE))
                musicNameList.add(musicName)
                Log.d("MusicService", "getMusicList: $musicPath name: $musicName")
            }
        }
    }
}
